<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:com="http://xml.netbeans.org/schema/comments" xmlns:pro="http://xml.netbeans.org/schema/products" xmlns:user="http://xml.netbeans.org/schema/users" xmlns:userdetail="http://xml.netbeans.org/schema/userdetails" exclude-result-prefixes="com pro user userdetail" version="1.0">
    <xsl:output method="html" indent="yes" version="1.0"/>
    <xsl:param name="type" select="'control'"/>
    <xsl:param name="contextpath" select="''"/>
    <xsl:param name="proid" select="''"/>
    <xsl:param name="propath" select="''"/>
    <xsl:param name="userpath" select="''"/>
    <xsl:param name="userdetailpath" select="''"/>

    <xsl:variable name = "prodoc" select="document($propath)"/>
    <xsl:variable name = "userdetaildoc" select="document($userdetailpath)"/>
    <xsl:variable name = "userdoc" select="document($userpath)"/>

    <!-- TODO customize transformation rules
         syntax recommendation http://www.w3.org/TR/xslt
    -->
    <xsl:template match="/">
        <xsl:if test="$type='control'">
            <xsl:call-template name="control"/>
        </xsl:if>
        <xsl:if test="$type='COMMENT_LOAD'">
            <xsl:call-template name="COMMENT_LOAD"/>
        </xsl:if>
    </xsl:template>
    <xsl:template name="control">
        <div id="CommentContainer">
            <h3>Product
                <xsl:value-of select="$prodoc/pro:Products/pro:Product[pro:Id=$proid]/pro:Name"/>
            </h3>
            <table border="1" class="table table-hover">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>User name</th>
                        <th>Comment</th>
                        <th>Rating</th>
                        <th>Timestamp</th>
                        <th>IsActive</th>
                        <th>Activation</th>
                    </tr>
                </thead>
                <tbody>
                    <xsl:for-each select="com:Comments/com:Comment[com:ProductId=$proid]">
                        <xsl:sort select="com:Timestamp" order="descending" data-type="Date"/>
                        <tr>
                            <td class="comProId">
                                <xsl:value-of select="com:ProductId"/>
                            </td>
                            <xsl:variable name="userid" select="com:UserId"/>
                            <xsl:variable name="userNode" select="$userdoc/user:Users/user:User[user:Id=$userid]"/>
                            <xsl:variable name="userdetalNode" select="$userdetaildoc/userdetail:UserDetails/userdetail:UserDetail[userdetail:Id=$userNode/user:UserDetailId]"/>
                            <td class="comUserId">
                                <xsl:value-of select="$userdetalNode/userdetail:Name"/>
                            </td>
                            <td class="comComment">
                                <xsl:value-of select="com:Comment"/>
                            </td>
                            <td class="comRating">
                                <xsl:value-of select="com:Rating"/>
                            </td>
                            <td class="comTimestamp">
                                <xsl:value-of select="com:Timestamp"/>
                            </td>
                            <td class="comActive">
                                <xsl:value-of select="com:IsActive"/>
                            </td>
                            <td class="comActivation">
                                <a>
                                    <xsl:attribute name="comid">
                                        <xsl:value-of select="com:Id"/>
                                    </xsl:attribute>
                                    <xsl:attribute name="href">
                                        <xsl:text>#</xsl:text>
                                    </xsl:attribute>
                                    <xsl:attribute name="class">
                                        <xsl:text>btn btn-info linkDelete</xsl:text>
                                    </xsl:attribute>
                                    <xsl:text>Delete</xsl:text>
                                </a>
                            </td>
                        </tr>
                    </xsl:for-each>
                </tbody>
            </table>
        </div>

    </xsl:template >

    <xsl:template name="COMMENT_LOAD">        
        <div id="error-container"></div>
        <div id="CommentContainer">
            <h3 class="comment-label">
                <xsl:variable name="proNode" select="$prodoc/pro:Products/pro:Product[pro:Id = $proid]"/>
                <xsl:value-of select="$proNode/pro:Name"/>
            Have
                <xsl:value-of select="count(com:Comments/com:Comment[com:ProductId=$proid])"/> Comment
            </h3>
            <textarea id="comment-text" class="comment-text"></textarea>
            <br/>
            <a>
                <xsl:attribute name="href">
                    <xsl:text>#</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="class">
                    <xsl:text>btn btn-success btn_comment</xsl:text>
                </xsl:attribute>
                <xsl:text>Add Comment</xsl:text>
            </a>
            <br/>
            <table border="1" class="table table-hover">
                <tr>
                    <th>Name</th>
                    <th>Comments</th>
                </tr>
                <xsl:for-each select="com:Comments/com:Comment[com:ProductId = $proid and com:IsActive='true']">
                    <xsl:sort select="com:Timestamp" order="descending" data-type="Date"/>
                    <xsl:variable name="userid" select="com:UserId"/>
                    <xsl:variable name="userNode" select="$userdoc/user:Users/user:User[user:Id = $userid]"/>
                    <xsl:variable name="userdetailid" select="$userNode/user:UserDetailId"/>
                    <xsl:variable name="userdetailNode" select="$userdetaildoc/userdetail:UserDetails/userdetail:UserDetail[userdetail:Id =$userdetailid]"/>

                    <tr>
                        <td>
                            <xsl:value-of select="$userdetailNode/userdetail:Name"/>
                            <xsl:text> </xsl:text>
                            <xsl:value-of select="$userdetailNode/userdetail:Lastname"/>
                        </td>
                        <td>
                            <xsl:value-of select="com:Comment"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </div>      
    </xsl:template>


</xsl:stylesheet>
